package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/construct-binary-search-tree-from-preorder-traversal/
 * @create 2025/9/25 20:01
 * @since JDK17
 */

public class Solution62 {
    int n;
    TreeNode dfs(int[] p, int max) {
        if (n == p.length) {
            return null;
        }
        int cur = p[n];
        if (cur > max) {
            return null;
        }
        n++;
        TreeNode node = new TreeNode(cur);
        node.left = dfs(p, cur);
        node.right = dfs(p, max);
        return node;
    }
    public TreeNode bstFromPreorder(int[] preorder) {
        return dfs(preorder, Integer.MAX_VALUE);
    }
}
